package com.zjw.project.weixin.api.accessToken;

import com.zjw.project.weixin.main.MenuManager;
import com.zjw.project.weixin.util.WeixinUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;


@Slf4j
public class AccessTokenThread extends Thread{

    private static String ACCESS_TOKEN_VAL;
    private static long ACCESS_TOKEN_GEN_TIME_MILLIS=3600000;



    @Override
    public void run(){
        while (true){
            ACCESS_TOKEN_VAL = this.getAccessTokenVal();
            System.out.println("生成的最新ACCESS_TOKEN值是："+ACCESS_TOKEN_VAL);
            try {
                Thread.sleep(ACCESS_TOKEN_GEN_TIME_MILLIS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static String TOKEN_GET_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    public String getAccessTokenVal(){

        String url = TOKEN_GET_URL.replace("APPID", MenuManager.appId).replace("APPSECRET",MenuManager.appSecret);


        try{
            JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
            String accessToken = jsonObject.getString("access_token");
            log.info("accessToken的值获取成功"+accessToken);
            return accessToken;
        }catch (Exception e){
            log.error(e.getMessage());
        }


        return null;
    }

}
